<?php

/**
 * contact : 刘经理 19326416371
 * company : 版权所属 四川北星惯牌科技有限公司
 **/

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Wechat;
use app\common\manager\BaseManager;
use app\common\manager\EmployeeManager;
use app\common\manager\GroupManager;
use app\common\manager\StoreAccountManager;
use app\common\manager\StoreManager;
use app\common\model\StoreInfo;
use app\common\model\Team;
use think\Exception;

/**
 * 门店信息
 */
class Store extends Api
{
    protected $noNeedLogin = ['list','detail','getTeam'];
    protected $noNeedRight = ['*'];

    /**
     * 获取附近门店
     * @lat number 纬度
     * @lng number 经度
     * @radius int 半径(公里)
     * @page int 页码
     * @return void
     */
    public function list()
    {
        $lat = $this->request->post('lat');
        $lng = $this->request->post('lng');
        $radius = $this->request->post('radius');
        $page = $this->request->post('page') ?? 1;
        try {
            $result = StoreManager::List($this->user,$lng,$lat,$radius,$page);
            if ($result['code'] == BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取门店列表
     * @return void
     */
    public function storeList(){
        $page = $this->request->post('page') ?? 1;
        try {
            $result = StoreManager::ListByUser($this->user,$page);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取商户信息
     * @return void
     */
    public function merchantInfo()
    {
        try {
            $result = StoreManager::merchantInfo($this->user);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取门店的邀请码
     * @param $id 门店id
     * @return void
     */
    public function invite()
    {
        $user = $this->user;
        $store_id = $this->request->post('id');
        try {
            $storeD = \app\common\model\Store::where('id',$store_id)->find();
            if ($user->merchant_id != $storeD['merchant_id']){
                throw new Exception('您无权执行此操作');
            }

            $wechat = new Wechat();
            $page = config('wechat.store_invite_page');
            $scene = ['t'=>2,'i'=>$storeD['invite_code'],'d'=>$store_id];
            $image = $result = $wechat->getUnlimited(http_build_query($scene),$page);
            $result = json_decode($result,true);
            if (json_last_error() == JSON_ERROR_NONE){
                if (isset($result['errcode']) && !empty($result['errcode'])){
                    throw new Exception($result['errmsg']);
                }
            }else{
                $this->success('获取成功',['image'=>base64_encode($image)]);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 用过id获取门店组队
     * @param $id 门店id
     * @param $status 状态:2=未开始,3=进行中,4=已结束,5=已取消
     * @param $page 页码
     * @return void
     */
    public function team()
    {
        $store_id = $this->request->post('id');
        $status = $this->request->post('status');
        $page  = $this->request->post('page') ?? 1;
        try {
            $result = GroupManager::storeTeamList($store_id,$status,$page);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取门店账户
     * @param $id 门店id
     * @return void
     */
    public function account()
    {
        $store_id = $this->request->post('id');
        try {
            $result = EmployeeManager::authCheck($this->user->id,$store_id,Team::IS_STORE_YES,null,EmployeeManager::TYPE_DEFAULT);

            if($result['code']!=BaseManager::$successCode){
                throw new Exception($result['message']);
            }

            $resultAccount = StoreAccountManager::getAccount($store_id);
            if ($resultAccount['code']==BaseManager::$successCode){
                $this->success($resultAccount['message'],$resultAccount['data']);
            }else{
                throw new Exception($resultAccount['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取金额日志
     * @param int $id 门店id
     * @param int $type 0=全部,1=支出,2=收入
     * @param int $detail 0=全部,1=奖励,2=冻结,3=提现,4=解冻,5=组队,6=赛事
     * @param int $page 页码
     * @return void
     */
    public function moneyLog()
    {
        $store_id = $this->request->post('id');
        $type = $this->request->post('type');
        $detail = $this->request->post('detail');
        $page = $this->request->post('page') ?? 1;
        if ($page)
        {
            $page = 1;
        }
        try {
            $result = EmployeeManager::authCheck($this->user->id,$store_id,Team::IS_STORE_YES,null,EmployeeManager::TYPE_DEFAULT);

            if($result['code']!=BaseManager::$successCode){
                throw new Exception($result['message']);
            }

            $where = [];
            $resultAccount = StoreAccountManager::moneyLog($store_id,$type,$detail,$where,$page);
            if ($resultAccount['code']==BaseManager::$successCode){
                $this->success($resultAccount['message'],$resultAccount['data']);
            }else{
                throw new Exception($resultAccount['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取门店详情
     * @param int $id 门店id
     * @param float $lng 经度
     * @param float $lat 纬度
     * @return void
     */
    public function detail()
    {
        $id = $this->request->post('id');
        $lng = $this->request->post('lng') ?? 0.00;
        $lat = $this->request->post('lat') ?? 0.00;
        try {
            $result = StoreManager::detail($id,$lng,$lat);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取组队信息
     * @param int $id 门店id
     * @param int $page 页码
     * @return void
     */
    public function getTeam()
    {
        $id = $this->request->post('id');
        $page = $this->request->post('page');
        try {
            $result = GroupManager::storeTeamListByUser($id,$page);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $exception){
            $this->error($exception->getMessage());
        }
    }

    /**
     * 门店入驻
     * @return void
     */
    public function merchantJoinIn(){
        $company_name = $this->request->post('company_name');
        $legal_person = $this->request->post('legal_person');
        $id_card = $this->request->post('id_card');
        $id_card_front_image = $this->request->post('id_card_front_image');
        $id_card_back_image = $this->request->post('id_card_back_image');
        $icon = $this->request->post('icon');
        $description = $this->request->post('description');
        $credit_code = $this->request->post('credit_code');
        $business_license_image = $this->request->post('business_license_image');
        $address = $this->request->post('address');
        $phone = $this->request->post('phone');
        $banner_image = $this->request->post('banner_image');
        $data = [
            'company_name' => $company_name,
            'legal_person' => $legal_person,
            'id_card' => $id_card,
            'id_card_front_image' => $id_card_front_image,
            'id_card_back_image' => $id_card_back_image,
            'icon' => $icon,
            'description' => $description,
            'credit_code' => $credit_code,
            'business_license_image' => $business_license_image,
            'address' => $address,
            'phone' => $phone,
            'mobile' => $this->user->mobile,
            'banner_image' => $banner_image,
        ];
        try {
            $result = StoreManager::merchantJoinIn($this->user,$data);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message']);
            }else{
                throw new Exception($result['message']);
            }
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    public function getMerchantApplyDetail()
    {
        try {
            $result = StoreManager::getMerchantApplyInfo($this->user);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message'],$result['data']);
            }else {
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    public function setAssistantRate()
    {
        $rate = $this->request->post('rate');
        $id = $this->request->post('id');
        try {
            $result = StoreManager::setAssistantRate($this->user,$id,$rate);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message']);
            }else {
                throw new Exception($result['message']);
            }
        }catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    public function writeOff()
    {
        $verification_code = $this->request->post('verification_code');
        try {
            $result = GroupManager::writeOff($this->user,$verification_code);
            if ($result['code']==BaseManager::$successCode){
                $this->success($result['message']);
            }else {
                throw new Exception($result['message']);
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

}
